package com.region.common.utils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author liujeiyu
 * @date 2023/12/2 13:40
 * @description
 */
public class ArrayUtils {

    /**
     * Getting the Cartesian Product between Multiple Arrays
     *
     * @param args
     * @return
     */
    public static List<List> descartes(List<List> args) {
        List<List> result = new LinkedList<>();
        if (args == null || args.size() == 0) {
            return Collections.EMPTY_LIST;
        }
        if (args.size() == 1) {
            result.add(args.get(0));
            return result;
        }
        List preList = args.get(0);
        for (int i = 1; i < args.size(); i++) {
            List list_i = args.get(i);
            List tempList = new LinkedList();
            for (int j = 0; j < preList.size(); j++) {
                for (int k = 0; k < list_i.size(); k++) {
                    List cut = new LinkedList();
                    if (preList.get(j) instanceof List) {
                        cut.addAll((List) preList.get(j));
                    } else {
                        cut.add(preList.get(j));
                    }
                    cut.add(list_i.get(k));
                    tempList.add(cut);
                }
            }
            preList = tempList;
            if (i >= args.size() - 1) {
                result = tempList;
            }
        }
        return result;
    }

}
